home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 February (DVD) / PCWorld_2008-02_DVD.iso / v cisle / PHP / PHP.exe / xampp-win32-1.6.5-installer.exe / phpMyAdmin / libraries / charset_conversion.lib.php < prev    next >
Encoding:
PHP Script  |  2007-12-20  |  11.4 KB  |  325 lines

  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4.  * Charset conversion functions.
  5.  *
  6.  * @version $Id: charset_conversion.lib.php 10240 2007-04-01 11:02:46Z cybot_tm $
  7.  */
  8.  
  9.  
  10. /**
  11.  * Loads the recode or iconv extensions if any of it is not loaded yet
  12.  */
  13. if (isset($cfg['AllowAnywhereRecoding'])
  14.     && $cfg['AllowAnywhereRecoding']
  15.     && $allow_recoding) {
  16.  
  17.     if ($cfg['RecodingEngine'] == 'recode') {
  18.         if (!@extension_loaded('recode')) {
  19.             PMA_dl('recode');
  20.             if (!@extension_loaded('recode')) {
  21.                 echo $strCantLoadRecodeIconv;
  22.                 exit;
  23.             }
  24.         }
  25.         $PMA_recoding_engine             = 'recode';
  26.     } elseif ($cfg['RecodingEngine'] == 'iconv') {
  27.         if (!@extension_loaded('iconv')) {
  28.             PMA_dl('iconv');
  29.             if (!@extension_loaded('iconv')) {
  30.                 echo $strCantLoadRecodeIconv;
  31.                 exit;
  32.             }
  33.         }
  34.         $PMA_recoding_engine             = 'iconv';
  35.     } else {
  36.         if (@extension_loaded('iconv')) {
  37.             $PMA_recoding_engine         = 'iconv';
  38.         } elseif (@extension_loaded('recode')) {
  39.             $PMA_recoding_engine         = 'recode';
  40.         } else {
  41.             PMA_dl('iconv');
  42.             if (!@extension_loaded('iconv')) {
  43.                 PMA_dl('recode');
  44.                 if (!@extension_loaded('recode')) {
  45.                     echo $strCantLoadRecodeIconv;
  46.                     exit;
  47.                 } else {
  48.                     $PMA_recoding_engine = 'recode';
  49.                 }
  50.             } else {
  51.                 $PMA_recoding_engine     = 'iconv';
  52.             }
  53.         }
  54.     }
  55. } // end load recode/iconv extension
  56.  
  57. define('PMA_CHARSET_NONE', 0);
  58. define('PMA_CHARSET_ICONV', 1);
  59. define('PMA_CHARSET_LIBICONV', 2);
  60. define('PMA_CHARSET_RECODE', 3);
  61. define('PMA_CHARSET_ICONV_AIX', 4);
  62.  
  63. if (!isset($cfg['IconvExtraParams'])) {
  64.     $cfg['IconvExtraParams'] = '';
  65. }
  66.  
  67. // Finally detects which function will we use:
  68. if (isset($cfg['AllowAnywhereRecoding'])
  69.     && $cfg['AllowAnywhereRecoding']
  70.     && $allow_recoding) {
  71.  
  72.     if (!isset($PMA_recoding_engine)) {
  73.         $PMA_recoding_engine = $cfg['RecodingEngine'];
  74.     }
  75.     if ($PMA_recoding_engine == 'iconv') {
  76.         if (@function_exists('iconv')) {
  77.             if ((@stristr(PHP_OS, 'AIX')) && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
  78.                 $PMA_recoding_engine = PMA_CHARSET_ICONV_AIX;
  79.             } else {
  80.                 $PMA_recoding_engine = PMA_CHARSET_ICONV;
  81.             }
  82.         } elseif (@function_exists('libiconv')) {
  83.             $PMA_recoding_engine = PMA_CHARSET_LIBICONV;
  84.         } else {
  85.             $PMA_recoding_engine = PMA_CHARSET_NONE;
  86.  
  87.             if (!isset($GLOBALS['is_header_sent'])) {
  88.                 include './libraries/header.inc.php';
  89.             }
  90.             echo $strCantUseRecodeIconv;
  91.             require_once './libraries/footer.inc.php';
  92.             exit();
  93.         }
  94.     } elseif ($PMA_recoding_engine == 'recode') {
  95.         if (@function_exists('recode_string')) {
  96.             $PMA_recoding_engine = PMA_CHARSET_RECODE;
  97.         } else {
  98.             $PMA_recoding_engine = PMA_CHARSET_NONE;
  99.  
  100.             require_once './libraries/header.inc.php';
  101.             echo $strCantUseRecodeIconv;
  102.             require_once './libraries/footer.inc.php';
  103.             exit;
  104.         }
  105.     } else {
  106.         if (@function_exists('iconv')) {
  107.             if ((@stristr(PHP_OS, 'AIX')) && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
  108.                 $PMA_recoding_engine = PMA_CHARSET_ICONV_AIX;
  109.             } else {
  110.                 $PMA_recoding_engine = PMA_CHARSET_ICONV;
  111.             }
  112.         } elseif (@function_exists('libiconv')) {
  113.             $PMA_recoding_engine = PMA_CHARSET_LIBICONV;
  114.         } elseif (@function_exists('recode_string')) {
  115.             $PMA_recoding_engine = PMA_CHARSET_RECODE;
  116.         } else {
  117.             $PMA_recoding_engine = PMA_CHARSET_NONE;
  118.  
  119.             require_once './libraries/header.inc.php';
  120.             echo $strCantUseRecodeIconv;
  121.             require_once './libraries/footer.inc.php';
  122.             exit;
  123.         }
  124.     }
  125. } else {
  126.     $PMA_recoding_engine         = PMA_CHARSET_NONE;
  127. }
  128.  
  129. /* Load AIX iconv wrapper if needed */
  130. if ($PMA_recoding_engine == PMA_CHARSET_ICONV_AIX) {
  131.     require_once './libraries/iconv_wrapper.lib.php';
  132. }
  133.  
  134. /**
  135.  * Converts encoding according to current settings.
  136.  *
  137.  * @param   mixed    what to convert (string or array of strings or object returned by mysql_fetch_field)
  138.  *
  139.  * @return  string   converted string or array of strings
  140.  *
  141.  * @global  array    the configuration array
  142.  * @global  boolean  whether recoding is allowed or not
  143.  * @global  string   the current charset
  144.  * @global  array    the charset to convert to
  145.  *
  146.  * @access  public
  147.  *
  148.  * @author  nijel
  149.  */
  150. function PMA_convert_display_charset($what) {
  151.     global $cfg, $allow_recoding, $charset, $convcharset;
  152.  
  153.     if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)
  154.         || $convcharset == $charset // rabus: if input and output charset are the same, we don't have to do anything...
  155.         // this constant is not defined before the login:
  156.         || (defined('PMA_MYSQL_INT_VERSION') && PMA_MYSQL_INT_VERSION >= 40100)) {  // lem9: even if AllowAnywhereRecoding is TRUE, do not recode for MySQL >= 4.1.x since MySQL does the job
  157.         return $what;
  158.     } elseif (is_array($what)) {
  159.         $result = array();
  160.         foreach ($what AS $key => $val) {
  161.             if (is_string($val) || is_array($val)) {
  162.                 if (is_string($key)) {
  163.                     $result[PMA_convert_display_charset($key)] = PMA_convert_display_charset($val);
  164.                 } else {
  165.                     $result[$key] = PMA_convert_display_charset($val);
  166.                 }
  167.             } else {
  168.                 $result[$key]     = $val;
  169.             }
  170.         } // end while
  171.         return $result;
  172.     } elseif (is_string($what)) {
  173.  
  174.         switch ($GLOBALS['PMA_recoding_engine']) {
  175.             case PMA_CHARSET_RECODE:
  176.                 return recode_string($convcharset . '..'  . $charset, $what);
  177.             case PMA_CHARSET_ICONV:
  178.                 return iconv($convcharset, $charset . $cfg['IconvExtraParams'], $what);
  179.             case PMA_CHARSET_ICONV_AIX:
  180.                 return PMA_aix_iconv_wrapper($convcharset, $charset . $cfg['IconvExtraParams'], $what);
  181.             case PMA_CHARSET_LIBICONV:
  182.                 return libiconv($convcharset, $charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
  183.             default:
  184.                 return $what;
  185.         }
  186.     } elseif (is_object($what)) {
  187.         // isn't it object returned from mysql_fetch_field ?
  188.         if (@is_string($what->name)) {
  189.             $what->name = PMA_convert_display_charset($what->name);
  190.         }
  191.         if (@is_string($what->table)) {
  192.             $what->table = PMA_convert_display_charset($what->table);
  193.         }
  194.         if (@is_string($what->Database)) {
  195.             $what->Database = PMA_convert_display_charset($what->Database);
  196.         }
  197.         return $what;
  198.     } else {
  199.         // when we don't know what it is we don't touch it...
  200.         return $what;
  201.     }
  202. } //  end of the "PMA_convert_display_charset()" function
  203.  
  204.  
  205. /**
  206.  * Converts encoding of text according to current settings.
  207.  *
  208.  * @param   string   what to convert
  209.  *
  210.  * @return  string   converted text
  211.  *
  212.  * @global  array    the configuration array
  213.  * @global  boolean  whether recoding is allowed or not
  214.  * @global  string   the current charset
  215.  * @global  array    the charset to convert to
  216.  *
  217.  * @access  public
  218.  *
  219.  * @author  nijel
  220.  */
  221. function PMA_convert_charset($what) {
  222.     global $cfg, $allow_recoding, $charset, $convcharset;
  223.  
  224.     if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)
  225.         || $convcharset == $charset) { // rabus: if input and output charset are the same, we don't have to do anything...
  226.         return $what;
  227.     } else {
  228.         switch ($GLOBALS['PMA_recoding_engine']) {
  229.             case PMA_CHARSET_RECODE:
  230.                 return recode_string($charset . '..'  . $convcharset, $what);
  231.             case PMA_CHARSET_ICONV:
  232.                 return iconv($charset, $convcharset . $cfg['IconvExtraParams'], $what);
  233.             case PMA_CHARSET_ICONV_AIX:
  234.                 return PMA_aix_iconv_wrapper($charset, $convcharset . $cfg['IconvExtraParams'], $what);
  235.             case PMA_CHARSET_LIBICONV:
  236.                 return libiconv($charset, $convcharset . $GLOBALS['cfg']['IconvExtraParams'], $what);
  237.             default:
  238.                 return $what;
  239.         }
  240.     }
  241. } //  end of the "PMA_convert_charset()" function
  242.  
  243. /**
  244.  * Converts encoding of text according to parameters with detected
  245.  * conversion function.
  246.  *
  247.  * @param   string   source charset
  248.  * @param   string   target charset
  249.  * @param   string   what to convert
  250.  *
  251.  * @return  string   converted text
  252.  *
  253.  * @access  public
  254.  *
  255.  * @author  nijel
  256.  */
  257. function PMA_convert_string($src_charset, $dest_charset, $what) {
  258.     if ($src_charset == $dest_charset) {
  259.         return $what;
  260.     }
  261.     switch ($GLOBALS['PMA_recoding_engine']) {
  262.         case PMA_CHARSET_RECODE:
  263.             return recode_string($src_charset . '..'  . $dest_charset, $what);
  264.         case PMA_CHARSET_ICONV:
  265.             return iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
  266.         case PMA_CHARSET_ICONV_AIX:
  267.             return PMA_aix_iconv_wrapper($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
  268.         case PMA_CHARSET_LIBICONV:
  269.             return libiconv($src_charset, $dest_charset, $what);
  270.         default:
  271.             return $what;
  272.     }
  273. } //  end of the "PMA_convert_string()" function
  274.  
  275.  
  276. /**
  277.  * Converts encoding of file according to parameters with detected
  278.  * conversion function. The old file will be unlinked and new created and
  279.  * its file name is returned.
  280.  *
  281.  * @param   string   source charset
  282.  * @param   string   target charset
  283.  * @param   string   file to convert
  284.  *
  285.  * @return  string   new temporay file
  286.  *
  287.  * @access  public
  288.  *
  289.  * @author  nijel
  290.  */
  291. function PMA_convert_file($src_charset, $dest_charset, $file) {
  292.     switch ($GLOBALS['PMA_recoding_engine']) {
  293.         case PMA_CHARSET_RECODE:
  294.         case PMA_CHARSET_ICONV:
  295.         case PMA_CHARSET_LIBICONV:
  296.             $tmpfname = tempnam('', 'PMA_convert_file');
  297.             $fin      = fopen($file, 'r');
  298.             $fout     = fopen($tmpfname, 'w');
  299.             if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_RECODE) {
  300.                 recode_file($src_charset . '..'  . $dest_charset, $fin, $fout);
  301.             } else {
  302.                 while (!feof($fin)) {
  303.                     $line = fgets($fin, 4096);
  304.                     if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV) {
  305.                         $dist = iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
  306.                     } elseif ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV_AIX) {
  307.                         $dist = PMA_aix_iconv_wrapper($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
  308.                     } else {
  309.                         $dist = libiconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
  310.                     }
  311.                     fputs($fout, $dist);
  312.                 } // end while
  313.             }
  314.             fclose($fin);
  315.             fclose($fout);
  316.             unlink($file);
  317.  
  318.             return $tmpfname;
  319.         default:
  320.             return $file;
  321.     }
  322. } //  end of the "PMA_convert_file()" function
  323.  
  324. ?>
  325.